<?php

namespace app\company\controller;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Db;
class Review extends Base
{
    private $where = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->where = ['com_id' => $this->com_id];
    }

    /**
     * 事件视图
     */
    public function index()
    {
        $this->set_tem_val();
        $params = [
            'theme' => trim(input('theme', '', 'addslashes')),
            'event_id' => input('event_id', 0, 'intval'),
            'department_id' => input('department_id', 0, 'intval'),
            'user_id' => input('user_id', 0, 'intval'),
            'prize_buckle_time' => input('prize_buckle_time', date('Y-m-1') . ' - ' . date('Y-m-d'), 'addslashes'),
            'status' => input('status', -1, 'intval'),
            'record_user_id' => input('record_user_id', 0, 'intval'),
            'first_check_user_id' => input('first_check_user_id', 0, 'intval'),
            'last_check_user_id' => input('last_check_user_id', 0, 'intval'),
            'integral_a_start' => input('integral_a_start', ''),
            'integral_a_end' => input('integral_a_end', ''),
            'integral_b_start' => input('integral_b_start', ''),
            'integral_b_end' => input('integral_b_end', ''),
            'is_lottery_ticket' => input('is_lottery_ticket', -1, 'intval')
        ];
        $where = ['ieru.com_id' => $this->com_id];
        //去除管理员
        $where['u.role_id'] = ['neq',''];
        $where['ipb.status'] = 2;
        $params['theme'] && $where['ipb.theme'] = ['like', '%' . $params['theme'] . '%'];
        $params['event_id'] && $where['ipbre.event_id'] = $params['event_id'];
        $params['department_id'] && $where['u.department_id'] = $params['department_id'];
        $params['user_id'] && $where['ieru.user_id'] = $params['user_id'];
        $prize_buckle_time = explode(' - ', $params['prize_buckle_time']);
        $where['ipb.prize_buckle_time'] = [['>=', strtotime($prize_buckle_time[0])], ['<=', strtotime($prize_buckle_time[1])], 'and'];
        ($params['status'] != -1) && $where['ieru.is_cancellation'] = $params['status'];
        $params['record_user_id'] && $where['ipb.record_user_id'] = $params['record_user_id'];
        $params['first_check_user_id'] && $where['ipb.first_check_user_id'] = $params['first_check_user_id'];
        $params['last_check_user_id'] && $where['ipb.last_check_user_id'] = $params['last_check_user_id'];
        //A分区间
        if (is_numeric($params['integral_a_start']) && is_numeric($params['integral_a_end'])) {
            $where['ieru.integral_a'] = [['>=', $params['integral_a_start']], ['<=', $params['integral_a_end']]];
        } elseif (is_numeric($params['integral_a_start'])) {
            $where['ieru.integral_a'] = ['>=', $params['integral_a_start']];
        } elseif (is_numeric($params['integral_a_end'])) {
            $where['ieru.integral_a'] = ['<=', $params['integral_a_end']];
        }
        //B分区间
        if (is_numeric($params['integral_b_start']) && is_numeric($params['integral_b_end'])) {
            $where['ieru.integral_b'] = [['>=', $params['integral_b_start']], ['<=', $params['integral_b_end']]];
        } elseif (is_numeric($params['integral_b_start'])) {
            $where['ieru.integral_b'] = ['>=', $params['integral_b_start']];
        } elseif (is_numeric($params['integral_b_end'])) {
            $where['ieru.integral_b'] = ['<=', $params['integral_b_end']];
        }
        ($params['is_lottery_ticket'] != -1) && $where['ieru.is_lottery_ticket'] = $params['is_lottery_ticket'];
//        var_dump($where);die;
        //导出
        if (input('export') == 1) {
            $list = db('com_integral_event_relation_users')->alias('ieru')
                ->join('com_integral_prize_buckle_relation_event ipbre', 'ipbre.id=ieru.event_id', 'LEFT')
                ->join('com_integral_prize_buckle ipb', 'ipb.id=ipbre.prize_buckle_id', "LEFT")
                ->join('com_event e', 'e.id=ipbre.event_id', 'LEFT')
                ->join('users u', 'u.id=ieru.user_id', 'LEFT')
                ->field([
                    'ieru.id', 'ieru.user_id',
                    'if(ieru.piece_rate>0,ieru.integral_b*ieru.piece_rate,ieru.integral_b) as integral_b',
                    'if(ieru.piece_rate>0,ieru.integral_a*ieru.piece_rate,ieru.integral_a) as integral_a',
                    'ieru.is_lottery_ticket',
                    'ieru.is_cancellation',
                    'ipb.id' => 'pb_id', 'ipb.prize_buckle_time', 'ipb.theme', 'ipb.record_user_id', 'ipb.first_check_user_id', 'ipb.last_check_user_id', 'ipb.status',
                    'e.name'
                ])
                ->where($where)->select();
            $this->index_explode($list);
        } else {
            $list = db('com_integral_event_relation_users')->alias('ieru')
                ->join('com_integral_prize_buckle_relation_event ipbre', 'ipbre.id=ieru.event_id', 'LEFT')
                ->join('com_integral_prize_buckle ipb', 'ipb.id=ipbre.prize_buckle_id', "LEFT")
                ->join('com_event e', 'e.id=ipbre.event_id', 'LEFT')
                ->join('users u', 'u.id=ieru.user_id', 'LEFT')
                ->field([
                    'ieru.id', 'ieru.user_id',
                    'if(ieru.piece_rate>0,ieru.integral_a*ieru.piece_rate,ieru.integral_a) as integral_a',
                    'if(ieru.piece_rate>0,ieru.integral_b*ieru.piece_rate,ieru.integral_b) as integral_b',
                    'ieru.is_lottery_ticket',
                    'ieru.is_cancellation',
                    'ipb.id' => 'pb_id', 'ipb.prize_buckle_time', 'ipb.theme', 'ipb.record_user_id', 'ipb.first_check_user_id', 'ipb.last_check_user_id', 'ipb.status',
                    'e.name'
                ])
                ->where($where)
                ->order('ipb.prize_buckle_time desc,ipbre.id desc')
                ->paginate('', false, page_param());
        }
//        var_dump($list);die;
        $data = [
            'list' => $list,//参与人列表
            'prize_buckle_time' => $params['prize_buckle_time'], //奖扣时间
        ];
        $this->assign($data);
        return $this->fetch();
    }

    //设置公共数据
    private function set_tem_val()
    {
        $list = db('com_department')->where($this->where)->select();  //部门列表
        $list = toTree($list);
        $list = organization_tree($list);
        $event_list = db('com_event')->where($this->where)->column('id, cate_id, name');  //事件列表
        $cate_ids = array_unique(array_column($event_list, 'cate_id')); //事件库分类id数组
        $cate_list = db('com_event_cate')->where($this->where)->column('id, pid, name');  //事件分类
        //参与人id
        $cy_ids = array_unique(db('com_integral_event_relation_users')->where('com_id', $this->com_id)->column('user_id'));
        $jcz_ids = db('com_integral_prize_buckle')->where('com_id', $this->com_id)->column('id, record_user_id, first_check_user_id, last_check_user_id');
        $jl_ids = array_unique(array_column($jcz_ids, 'record_user_id'));//记录人
        $cs_ids = array_unique(array_column($jcz_ids, 'first_check_user_id'));//初审人
        $zs_ids = array_unique(array_column($jcz_ids, 'last_check_user_id'));//终审人
        $user_ids = array_unique(array_merge($jl_ids, $cs_ids, $zs_ids));
        $user_list = db('users')->where('com_id',$this->com_id)->column('id, name');//所有用户列表
        $data = [
            'com_department' => $list,
            'event_list' => $event_list,
            'cate_list' => $cate_list,
            'cy_ids' => $cy_ids,
            'jl_ids' => $jl_ids,
            'cs_ids' => $cs_ids,
            'zs_ids' => $zs_ids,
            'user_list' => $user_list,
            'is_lottery_ticket' => [0 => '否', 1 => '是'],//是否奖票
            'status' => [0 => '待初审', 1 => '待终审', 2 => '审核通过', 3 => '作废', 4 => '驳回'],//状态
        ];
        $this->assign($data);
        return $data;
    }

    //导入
    public function index_import()
    {
        require EXTEND_PATH . 'PHPExcel/PHPExcel.php';
        require EXTEND_PATH . 'PHPExcel/PHPExcel/IOFactory.php';
        if ($_FILES['file']['error'] != 0) {
            return $this->ajaxReturn(1, "文件上传有误");
        }
        //文件是否大于2M
        if ($_FILES['file']['size'] > 102400*2) {
            return $this->ajaxReturn(1, "文件最大2M");
        }
        $r = action('file/controller_up');
        if ($r['err'] != 0) {
            return $this->ajaxReturn(1, "文件保存出错");
        }
        $path = ROOT_PATH . 'public' . $r['data']['saveName'];
        $type = 'Excel2007';//设置为Excel5代表支持2003或以下版本，Excel2007代表2007版
        $xlsReader = \PHPExcel_IOFactory::createReader($type);
        $xlsReader->setReadDataOnly(true);
        $xlsReader->setLoadSheetsOnly(true);

        $Sheets = $xlsReader->load($path);
        $dataArray = $Sheets->getSheet(0)->toArray();
        unlink($path);
        $data = [];
        foreach ($dataArray as $k => $v) {
            $group_name = trim($v[0]);
            $job_number = trim($v[1]);
            $user_name = trim($v[2]);
            if ($k > 1 && $group_name && $job_number && $user_name) {
                $data[] = [
                    'group_name' => $group_name, //分组名称
                    'job_number' => $job_number, //工号
                    'user_name' => $user_name   //用户姓名
                ];
            }
        }
        if (!$data) {
            return $this->ajaxReturn(1, "导入用户不能为空");
        }
        $com_statistics_group = db('com_statistics_group')->where($this->com_id)->column('name, id'); //部门列表
        $data_group_name = array_unique(array_column($data, 'group_name'));
        //检查表格中的部门是否有误
        foreach ($data_group_name as $k => $v) {
            if (!array_key_exists($v, $com_statistics_group)) {
                return $this->ajaxReturn(1, "导入失败 分组名称 {$v} 不存在");
            }
        }
        //检查人员信息是否有误
        $users = db('users')->where(['com_id' => $this->com_id, 'type' => 1])->column('name, id, job_number');//企业已有人员
        foreach ($data as $k => $v) {
            if (array_key_exists($v['user_name'], $users) && $v['job_number'] == $users[$v['user_name']]['job_number']) {
                $data[$k]['user_id'] = $users[$v['user_name']]['id'];
                $data[$k]['group_id'] = $com_statistics_group[$v['group_name']];
                $data[$k]['com_id'] = $this->com_id;
                unset($data[$k]['group_name'], $data[$k]['job_number'], $data[$k]['user_name']);
            } else {
                return $this->ajaxReturn(1, "导入失败 人员 {$v['user_name']} 信息有误或已离职");
            }
        }
        $group_relation_users = db('com_statistics_group_relation_users')->where('com_id', $this->com_id)
            ->field('group_id, user_id')->select();
        //过滤已存在的分组关联用户
        $data = array_unique($data, SORT_REGULAR);
        foreach ($data as $k => $v) {
            foreach ($group_relation_users as $k2 => $v2) {
                if ($v2['group_id'] == $v['group_id'] && $v2['user_id'] == $v['user_id']) {
                    unset($data[$k]);
                }
            }
        }
        db('com_statistics_group_relation_users')->insertAll($data);
        return $this->ajaxReturn(0, "导入成功");
    }

    //下载导入模板
    public function down_exl()
    {
        $file_name = ROOT_PATH . 'public/static/quantify-template.xlsx';
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="quantify-template.xlsx"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file_name));
        readfile($file_name);

        exit;
    }

    //事件积分核查导出
    public function index_explode_1($list)
    {
        $data = $this->set_tem_val();
        $user_list = $data['user_list'];
        $is_lottery_ticket = $data['is_lottery_ticket'];
        $status = $data['status'];
        require EXTEND_PATH . 'Export.php';
        $key = ['奖扣对象', '奖扣时间', '主题名称', '事件名称', 'A分', 'B分', '奖票', '记录人', '初审人', '终审人', '状态'];
        $val = [];
        foreach ($list as $k => $v) {
            if(!isset($user_list[$v['user_id']])){
                continue;
            }
            $val[] = [
                $user_list[$v['user_id']],
                date('Y-m-d', $v['prize_buckle_time']),
                $v['theme'],
                $v['name'],
                $v['integral_a'],
                $v['integral_b'],
                $is_lottery_ticket[$v['is_lottery_ticket']],
                $user_list[$v['record_user_id']],
                $user_list[$v['first_check_user_id']],
                $user_list[$v['last_check_user_id']],
                $status[$v['status']],
            ];
        }
        new \Export($key, $val, '事件积分核查');
        die;
    }
    //事件积分核查导出@stone
    public function index_explode($list){
        set_time_limit(0);
        $data = $this->set_tem_val();
        $user_list = $data['user_list'];
        $is_lottery_ticket = $data['is_lottery_ticket'];
        $status = $data['status'];
        require EXTEND_PATH . 'Export.php';
        $key = ['奖扣对象', '奖扣时间', '主题名称', '事件名称', 'A分', 'B分', '奖票', '记录人', '初审人', '终审人', '状态'];
        import('PHPExcel/PHPExcel', EXTEND_PATH);
        $obj = new \PHPExcel();
        //横向单元格标识
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');

        $obj->getActiveSheet(0)->setTitle(date('Y-m-d',time()).'事件积分核查');   //设置sheet名称
        $_row = 1;   //设置纵向单元格标识
        $title=$key;
        if($title){
            $i = 0;
            foreach($title AS $v){   //设置列标题
                $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);

                $i++;
            }
            $_row++;
        }
        //设置样式
        $obj->getActiveSheet()->getStyle( 'A1:K1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        //设置背景样式
        $obj->getActiveSheet()->getStyle( 'A1:K1')->getFill()->getStartColor()->setARGB('FF808000');
        //设置单元格格式:文本
        $obj->getActiveSheet()->getStyle('B')->getNumberFormat()
            ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
        $obj->getActiveSheet()->getStyle('C')->getNumberFormat()
            ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
        //设置宽度
        $obj->getActiveSheet()->getColumnDimension( 'B')->setAutoSize(true);
//        $obj->getActiveSheet()->getColumnDimension( 'C')->setAutoSize(true);
        $obj->getActiveSheet()->getColumnDimension( 'E')->setAutoSize(true);
        $obj->getActiveSheet()->getColumnDimension( 'F')->setAutoSize(true);
//        $obj->getActiveSheet()->getColumnDimension( 'G')->setAutoSize(true);
//        $obj->getActiveSheet()->getColumnDimension( 'H')->setAutoSize(true);
        $obj->getActiveSheet()->getColumnDimension( 'C')->setWidth(30);
        $obj->getActiveSheet()->getColumnDimension( 'D')->setWidth(25);
        $obj->getActiveSheet()->getColumnDimension( 'G')->setWidth(5);
        $obj->getActiveSheet()->getColumnDimension( 'H')->setWidth(15);
        $obj->getActiveSheet()->getColumnDimension( 'I')->setWidth(15);
        $obj->getActiveSheet()->getColumnDimension( 'J')->setWidth(15);
        $obj->getActiveSheet()->getColumnDimension( 'K')->setWidth(15);
        //填写数据
        $val = [];
        foreach ($list as $k => $v) {
            if(!isset($user_list[$v['user_id']])){
                continue;
            }
            $val[] = [
                $user_list[$v['user_id']],
                date('Y-m-d', $v['prize_buckle_time']),
                $v['theme'],
                $v['name'],
                $v['integral_a'],
                $v['integral_b'],
                $is_lottery_ticket[$v['is_lottery_ticket']],
                $user_list[$v['record_user_id']]??'系统管理员',
                $user_list[$v['first_check_user_id']]??'系统管理员',
                $user_list[$v['last_check_user_id']]??'系统管理员',
                $status[$v['status']],
            ];
        }
        $data = $val;
        if($data){
            $i = 0;
            foreach($data AS $_v){
                foreach ($_v as $key=>$_vv){
                    $obj->getActiveSheet(0)->setCellValue($cellName[$key] . ($i+$_row), $_vv);
                }
                $i++;
            }
        }
        //文件名处理
        $fileName = date('Y-m-d',time()).'事件积分核查';
        $objWrite = \PHPExcel_IOFactory::createWriter($obj, 'Excel2007');
        //网页下载
        header('pragma:public');
        header("Content-Disposition:attachment;filename=$fileName.xlsx");
        $objWrite->save('php://output');exit;

    }

    //主题详情
    public function event_info()
    {
        $id = I('id', 0);
        if (empty($id)) {
            return $this->error('主题id不能为空');
        }
        //主题
        $theme = M('com_integral_prize_buckle a')
            ->join('users b', 'b.id = a.first_check_user_id', 'left')
            ->join('users c', 'c.id = a.last_check_user_id', 'left')
            ->join('users d', 'd.id = a.record_user_id', 'left')
            ->field('a.*,b.name b_name,c.name c_name,d.name d_name')
            ->where('a.id', $id)->find();
        //事件
        $event = M('com_integral_prize_buckle_relation_event a')
            ->join('com_event b', 'b.id=a.event_id', 'left')
            ->join('com_event_cate d', 'd.id=b.cate_id', 'left')
            ->join('com_integral_event_relation_users c', 'c.event_id = a.id', 'left')
            ->field('a.*,sum(c.integral_a) integral_a,sum(c.integral_b) integral_b,b.*,a.id id,d.name cate_name')
            ->group('a.id')
            ->where('a.prize_buckle_id', $id)->select();
        //参与人
        foreach ($event as $key => $value) {
            $users = M('com_integral_event_relation_users a')
                ->join('users b', 'b.id=a.user_id', 'left')
                ->where('a.event_id', $value['id'])
                ->field('a.*,b.name user_name')
                ->select();
            $event[$key]['user_list'] = $users;
        }
        //查询记录人奖励分数
        $integral = M('com_config')->where('com_id', $this->com_id)->value('day_add_integral');
        $this->assign('integral', $integral);
        $this->assign('theme', $theme);
        $this->assign('event', $event);
        return $this->fetch();
    }

    /**
     * 员工视图
     */
    public function staff()
    {
        //date("t",strtotime(date('Y-m'))) * 86400 当前月份的秒数

        $department_list = db('com_department')->where($this->where)->select();  //部门列表
        $department_list = toTree($department_list);
        $department_list = organization_tree($department_list);
        $params = [
            'department_id' => input('department_id', 0, 'intval'),
            'name' => input('name', '', 'addslashes'),
            'job_number' => input('job_number', '', 'addslashes'),
            'create_time' => input('create_time', date('Y-m') . ' - ' . date('Y-m'), 'addslashes')
        ];
        $where = ['ieru.com_id' => $this->com_id];
        $params['department_id'] && $where['u.department_id'] = $params['department_id'];
        $params['name'] && $where['u.name'] = ['like', '%' . $params['name'] . '%'];
        $params['job_number'] && $where['u.job_number'] = ['like', '%' . $params['job_number'] . '%'];
        $create_time = explode(' - ', $params['create_time']);
        $start_time = strtotime($create_time[0]);
        $end_time = strtotime($create_time[1]);
        $end_time = strtotime('+1 month',$end_time) -1;
        $where['a.prize_buckle_time'] = [['>=', $start_time], ['<=', $end_time], 'and'];
        $where['u.type'] = 1;
        $where['a.status'] = 2;
        $where['ieru.is_cancellation'] = 0;
//        dump($where);
//        die;

        //导出
        if (input('export') == 1) {
            $list = M('com_integral_event_relation_users ieru')
                ->join('com_integral_prize_buckle_relation_event ipbre', 'ipbre.id=ieru.event_id', 'LEFT')
                ->join('com_integral_prize_buckle a', 'a.id=ipbre.prize_buckle_id', 'LEFT')
                ->join('users u', 'u.id=ieru.user_id', 'LEFT')
                ->field([
                    'ieru.id',
                    'count(ieru.id)' => 'event_num',
                    'sum(if(ieru.piece_rate>0,ieru.integral_a*ieru.piece_rate,ieru.integral_a))' => 'integral_a',
                    'sum(if(ieru.piece_rate>0,ieru.integral_b*ieru.piece_rate,ieru.integral_b))' => 'integral_b',
                    'ipbre.create_time',
                    'from_unixtime(a.prize_buckle_time,"%Y-%m") as prize_buckle_time',
                    'u.id as u_id',
                    'u.name',
                    'u.job_number'
                ])
                ->where($where)->group('from_unixtime(a.prize_buckle_time,"%Y-%m") desc,u.id')->select();
            foreach ($list as $key=>&$value){

                $start = $value['prize_buckle_time'].'-01';
                $start_time = strtotime($start);
                $end_time = strtotime("$start +1 month")-1;
                $value['leave_time'] = $this->total_user_leave_time($value['u_id'],$start_time,$end_time)['name'];
            }
            $this->staff_explode($list);
        } else {
            $list = M('com_integral_event_relation_users ieru')
                ->join('com_integral_prize_buckle_relation_event ipbre', 'ipbre.id=ieru.event_id', 'LEFT')
                ->join('com_integral_prize_buckle a', 'a.id=ipbre.prize_buckle_id', 'LEFT')
                ->join('users u', 'u.id=ieru.user_id', 'LEFT')
                ->field([
                    'ieru.id',
                    'count(ieru.id)' => 'event_num',
                    'sum(if(ieru.piece_rate>0,ieru.integral_a*ieru.piece_rate,ieru.integral_a))' => 'integral_a',
                    'sum(if(ieru.piece_rate>0,ieru.integral_b*ieru.piece_rate,ieru.integral_b))' => 'integral_b',
                    'ipbre.create_time',
                    'from_unixtime(a.prize_buckle_time,"%Y-%m") as prize_buckle_time',
                    'u.id as u_id',
                    'u.name',
                    'u.job_number'
                ])
                ->where($where)->group('from_unixtime(a.prize_buckle_time,"%Y-%m") desc,u.id')->paginate('', false, page_param());
//            var_dump($list);die;
        }
        $list_a = $list->toArray()['data'];

        foreach ($list_a as $key=>&$value){
            $start = $value['prize_buckle_time'].'-01';
            $start_time = strtotime($start);
            $end_time = strtotime("$start +1 month")-1;
            $value['leave_time'] = $this->total_user_leave_time($value['u_id'],$start_time,$end_time)['name'];
            $value['hour'] = $this->total_user_leave_time($value['u_id'],$start_time,$end_time)['hour'];
            $value['day'] = $this->total_user_leave_time($value['u_id'],$start_time,$end_time)['day'];
        }
        $data = [
            'list' => $list,
            'list_a' => $list_a,
            'create_time' => $params['create_time'],
            'department_list' => $department_list
        ];
        $this->assign($data);
        return $this->fetch();
    }
    //统计用户请假时长
    public function total_user_leave_time($user_id,$start_time,$end_time){
        $where = [
            'user_id'=>$user_id,
            'status'=>2,
            'start_time'=>[['>=', $start_time], ['<=', $end_time], 'and']
        ];
        $day = M('com_leave')->where($where)->sum('day');
        $hour = M('com_leave')->where($where)->sum('hour');
        $data = [
            'name'=>$day.'天'.$hour.'小时',
            'hour'=>$hour,
            'day'=>$day,
        ];
        return $data;
//        return $day.'天'.$hour.'小时';
    }

    //员工视图导出
    public function staff_explode_1($list)
    {
        require EXTEND_PATH . 'Export.php';
        $key = ['月份', '奖扣对象', '工号', '事件数', 'A分', 'B分','请假时长'];
        $val = [];
        foreach ($list as $k => $v) {
            $val[] = [
                date('Y年m', $v['create_time']),
                $v['name'],
                $v['job_number'],
                $v['event_num'],
                $v['integral_a'],
                $v['integral_b'],
                $v['leave_time'],
            ];
        }
        new \Export($key, $val, '员工积分核查');
        die;
    }
    //员工视图导出@stone
    public function staff_explode($list){
        set_time_limit(0);
        $data = $this->set_tem_val();
        $user_list = $data['user_list'];
        $is_lottery_ticket = $data['is_lottery_ticket'];
        $status = $data['status'];
        require EXTEND_PATH . 'Export.php';
        $key = ['月份', '奖扣对象', '工号', '事件数', 'A分', 'B分','请假时长'];
        import('PHPExcel/PHPExcel', EXTEND_PATH);
        $obj = new \PHPExcel();
        //横向单元格标识
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');

        $obj->getActiveSheet(0)->setTitle(date('Y-m-d',time()).'员工积分核查');   //设置sheet名称
        $_row = 1;   //设置纵向单元格标识
        $title=$key;
        if($title){
            $i = 0;
            foreach($title AS $v){   //设置列标题
                $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);

                $i++;
            }
            $_row++;
        }
        //设置样式
        $obj->getActiveSheet()->getStyle( 'A1:G1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        //设置背景样式
        $obj->getActiveSheet()->getStyle( 'A1:G1')->getFill()->getStartColor()->setARGB('FF808000');
        //设置单元格格式:文本
        $obj->getActiveSheet()->getStyle('B')->getNumberFormat()
            ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
        $obj->getActiveSheet()->getStyle('C')->getNumberFormat()
            ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
        //设置宽度
//        $obj->getActiveSheet()->getColumnDimension( 'A')->setAutoSize(true);
//        $obj->getActiveSheet()->getColumnDimension( 'C')->setAutoSize(true);
        $obj->getActiveSheet()->getColumnDimension( 'A')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension( 'B')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension( 'C')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension( 'D')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension( 'E')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension( 'F')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension( 'G')->setWidth(20);
        //填写数据
        $val = [];
        foreach ($list as $k => $v) {
            $val[] = [
                date('Y年m', $v['create_time']),
                $v['name'],
                $v['job_number'],
                $v['event_num'],
                $v['integral_a'],
                $v['integral_b'],
                $v['leave_time'],
            ];
        }
        $data = $val;
        if($data){
            $i = 0;
            foreach($data AS $_v){
                foreach ($_v as $key=>$_vv){
                    $obj->getActiveSheet(0)->setCellValue($cellName[$key] . ($i+$_row), $_vv);
                }
                $i++;
            }
        }
        //文件名处理
        $fileName = date('Y-m-d',time()).'员工积分核查';
        $objWrite = \PHPExcel_IOFactory::createWriter($obj, 'Excel2007');
        //网页下载
        header('pragma:public');
        header("Content-Disposition:attachment;filename=$fileName.xlsx");
        $objWrite->save('php://output');exit;
    }

    //事件视图，导出   //todo 未用
    public function review_importxx()
    {
        $data = array(0 => array(0 => 1, 1 => 'alex1', 2 => 1,), 1 => array(0 => 2, 1 => 'alex2', 2 => 2,), 2 => array(0 => 3, 1 => 'alex3', 2 => 1,), 3 => array(0 => 4, 1 => 'alex4', 2 => 2,), 4 => array(0 => 5, 1 => 'alex5', 2 => 1,), 5 => array(0 => 6, 1 => 'alex6', 2 => 2,));

        $title = ['id', 'name', 'sex'];


        $spreadsheet = new Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();
//设置工作表标题名称
        $worksheet->setTitle('测试Excel');

//表头
//设置单元格内容
        foreach ($title as $key => $value) {
            $worksheet->setCellValueByColumnAndRow($key + 1, 1, $value);
        }

        $row = 2; //第二行开始
        foreach ($data as $item) {
            $column = 1;
            foreach ($item as $value) {
                $worksheet->setCellValueByColumnAndRow($column, $row, $value);
                $column++;
            }
            $row++;
        }

# 保存为xlsx
        $filename = '测试Excel.xlsx';
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save($filename);
# 浏览器下载
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer->save('php://output');
        exit();
    }

    //事件视图，导入 author:zj
    public function review_import()
    {
        if ($_FILES['file']['error'] != 0) {
            return $this->ajaxReturn(1, "文件上传有误");
        }
        //文件是否大于100KB
        if ($_FILES['file']['size'] > 102400) {
            return $this->ajaxReturn(1, "文件最大8M");
        }
        $r = action('file/controller_up');
        if ($r['err'] != 0) {
            return $this->ajaxReturn(1, "文件保存出错");
        }
        $path = ROOT_PATH . 'public' . $r['data']['saveName'];
        $type = 'Xlsx';//设置为Xls代表支持2003或以下版本，Xlsx代表2007版
        $xlsReader = IOFactory::createReader($type);
        $xlsReader->setReadDataOnly(true);
        $Sheets = $xlsReader->load($path);
        $dataArray = $Sheets->getSheet(0)->toArray();

        if (count($dataArray) > 200) {
            return $this->ajaxReturn(1, "每次导入最大条数为200条");
        }

        @unlink($path);      //删除文件

        $tmpArr = [];
        unset($dataArray[0]);       //删除第一行，从第二行开始读取
        foreach ($dataArray as $key=>$vv) {
            $msg = '第'.($key+1).'条数据';
            if (!$vv[0]) return $this->ajaxReturn(1, $msg.'主题不能为空');
            if (!$vv[1]) return $this->ajaxReturn(1, $msg.'备注不能为空');
            if (!$vv[2]) return $this->ajaxReturn(1, $msg.'人员不能为空');
            if (!$vv[3]) return $this->ajaxReturn(1, $msg.'人员工号不能为空');
            if (!strtotime($vv[4])) return $this->ajaxReturn(1, $msg.'时间不能为空或格式不正确');
            if (!$vv[5]) return $this->ajaxReturn(1, $msg.'事件类型不能为空');
            if (!$vv[6]) return $this->ajaxReturn(1, $msg.'事件名称不能为空');
            if (!$vv[7]) return $this->ajaxReturn(1, $msg.'描述不能为空');
            if (!$vv[8] && is_numeric($vv[8])) return $this->ajaxReturn(1, $msg.'A分不能为空,并且为数值');
            if (!$vv[9] && is_numeric($vv[9])) return $this->ajaxReturn(1, $msg.'B分不能为空,并且为数值');
            if (!$vv[10]) return $this->ajaxReturn(1, $msg.'初审人不能为空');
            if (!$vv[11]) return $this->ajaxReturn(1, $msg.'初审人工号不能为空');
            if (!$vv[12]) return $this->ajaxReturn(1, $msg.'终审人不能为空');
            if (!$vv[13]) return $this->ajaxReturn(1, $msg.'终审人工号不能为空');
            
            $md5 = md5($vv[0].$vv[1].$vv[4].$vv[10].$vv[11].$vv[12].$vv[13]);
            $tmpArr[$md5][] = $vv;
        }

        $time = time();
        foreach ($tmpArr as $key => $rows) {
            $num = count($rows);        //事件，人次数

            //取该主题下的最大绝对值A分
            $a_score = array_column($rows, '8');
            $aMin = min($a_score);
            $aMax = max($a_score);
            $a_score = abs($aMin) - abs($aMax) >= 0 ? $aMin : $aMax ;

            //取该主题下的最大绝对值B分
            $b_score = array_column($rows, '9');
            $bMin = min($b_score);
            $bMax = max($b_score);
            $b_score = abs($bMin) - abs($bMax) >= 0 ? $bMin : $bMax ;

            //判断初审人存不存在，初审人+工号
            if (!$fuserInfo = M('users')->where([
                'com_id' => $this->com_id,
                'name' => trim($rows[0][10]),
                'job_number' => trim($rows[0][11])
            ])->find()) {
                return $this->ajaxReturn(1, '未找到初审人姓名为' . $rows[0][10] . '，工号为' . $rows[0][11] . '的人');
            }
            //判断初审人是否有，审核权限
            if (!M('com_prize_buckle_relation_users')->where([
                'com_id' => $this->com_id,
                'user_id' => $fuserInfo['id']
            ])->find()) {
                return $this->ajaxReturn(1, '初审人' . $rows[0][10] . '没有审核权限');
            }

            //判断终审人存不存在，终审人+工号
            if (!$luserInfo = M('users')->where([
                'com_id' => $this->com_id,
                'name' => trim($rows[0][12]),
                'job_number' => trim($rows[0][13])
            ])->find()) {
                return $this->ajaxReturn(1, '未找到终审人姓名为' . $rows[0][12] . '，工号为' . $rows[0][13] . '的人');
            }
            //判断终审人是否有，审核权限
            $luser_prize = db('com_prize_buckle_relation_users cu')
                ->join('com_prize_buckle cb', 'cu.prize_buckle_id = cb.id', 'left')
                ->where('cu.com_id', $this->com_id)
                ->where('cb.com_id', $this->com_id)
                ->where('cu.user_id', $luserInfo['id'])
                ->column('cu.user_id, cb.integral_a, cb.integral_b');
            if (!$luser_prize && empty($luser_prize)) return $this->ajaxReturn(1, '终审人' . $rows[0][10] . '没有审核权限');

            //判断,主题+备注+时间+初审人+初审人工号+终审人+终审人工号，如果存在则取id使用，如果不存在，新建主题
            $cipb_Info = M('com_integral_prize_buckle')->where(
                [
                    'com_id' => $this->com_id,
                    'desc' => trim($rows[0][1]),
                    'theme' => trim($rows[0][0]),
                    'first_check_user_id' => $fuserInfo['id'],
                    'last_check_user_id' => $luserInfo['id'],
                    'record_user_id' => $this->user_id,
                ]
            )->find();
            //不存在，新建主题
            if (!$cipb_Info) {
                //新建，得到主题id
                $cipb_data = [
                    'com_id' => $this->com_id,
                    'first_check_user_id' => $fuserInfo['id'],
                    'last_check_user_id' => $luserInfo['id'],
                    'record_user_id' => $this->user_id,
                    'prize_buckle_time' => strtotime($rows[0][4]),
                    'status' => 2,
                    'first_check_time' => $time,
                    'last_check_time' => $time,
                    'theme' => trim($rows[0][0]),
                    'desc' => trim($rows[0][1]),
                    'create_time' => $time,
                ];
                $cipb_Info = $cipb_data;
//                $cipb_Id = M('com_integral_prize_buckle')->insertGetId($cipb_data);
            } else {
                $cipb_Id = $cipb_Info['id'];
            }

            foreach ($rows as $row) {

                //判断员工存不存在。人员+工号
                if (!$userInfo = M('users')->where([
                    'com_id' => $this->com_id,
                    'name' => trim($row[2]),
                    'job_number' => trim($row[3])
                ])->find()) {
                    return $this->ajaxReturn(1, '未找到人员姓名为' . $row[2] . '，工号为' . $row[3] . '的人员');
                }

                //查询事件类型
                if (!$eventCateInfo = M('com_event_cate')->where('name', trim($row[5]))->where('com_id', 'in', [0, $this->com_id])->find()) {
                    return $this->ajaxReturn(1, '未找到名称为' . $row[5] . '分类');
                }
                //查询事件，存在则获取Id，不存在则新建事件
                $eventInfo = M('com_event')->where([
//                'type'=>1,
                    'name' => trim($row[6]),
                    'cate_id' => $eventCateInfo['id'],
                    'desc' => trim($row[7])
                ])->find();

                if (!$eventInfo) {//未查到事件，去新建
                    $eventSave = [
                        'com_id' => $this->com_id,
                        'cate_id' => $eventCateInfo['id'],
                        'type' => 2,
                        'name' => trim($row[6]),
                        'first_check_user_id' => $fuserInfo['id'],
                        'last_check_user_id' => $luserInfo['id'],
                        'desc' => trim($row[7]),
                        'create_time' => $time
                    ];
//                $eventId = M('com_event')->insertGetId($eventSave);
                } else {
                    $eventId = $eventInfo['id'];
                }

                //判断审核权限和分值范围
                if ($eventInfo && $eventInfo['type'] == 1) {   //正式事件
                    if ($row[8] - $eventInfo['integral_a_start'] < 0 || $eventInfo['integral_a_end'] - $row[8] < 0) {   //超标事件
                        if (abs($luser_prize['integral_a']) < $a_score) {  //终审人A分小于填写A分则报错
                            return $this->ajaxReturn(1, '事件' . $row[6] . '所填写A分超出终审人审核权限');
                        }
                    }

                    if ($row[9] - $eventInfo['integral_b_start'] < 0 || $eventInfo['integral_b_end'] - $row[9] < 0) {   //超标事件
                        if (abs($luser_prize['integral_b']) < $b_score) {  //终审人A分小于填写A分则报错
                            return $this->ajaxReturn(1, '事件' . $row[6] . '所填写B分超出终审人审核权限');
                        }
                    }
                }

                //非正式事件或者新增临时事件
                if (!$eventInfo || $eventInfo['type'] != 1) {

                    if (abs($luser_prize['integral_a']) < $a_score) {  //终审人A分小于填写A分则报错
                        return $this->ajaxReturn(1, '事件' . $row[6] . '所填写A分超出终审人审核权限');
                    }

                    if (abs($luser_prize['integral_b']) < $b_score) {  //终审人A分小于填写A分则报错
                        return $this->ajaxReturn(1, '事件' . $row[6] . '所填写B分超出终审人审核权限');
                    }
                }

                DB::startTrans();
                try {
                    //新建事件
                    if (!$eventId && isset($eventSave) && !empty($eventSave)) {
                        $eventId = M('com_event')->insertGetId($eventSave);
                    }

                    //新建主题、更新主题分值
                    if (!$cipb_Id && isset($cipb_data) && !empty($cipb_data)) {
                        $cipb_Id = M('com_integral_prize_buckle')->insertGetId($cipb_data);
                    }
                    else{
                        if($row[8] > 0){
                            $cipdUpdate['integral_a_add'] = Db::raw('integral_a_del+'.$row[8]);
                        }else{
                            $cipdUpdate['integral_a_del'] = Db::raw('integral_a_del+'.$row[8]);
                        }
                        if($row[8] > 0){
                            $cipdUpdate['integral_b_add'] = Db::raw('integral_b_add+'.$row[9]);
                        }else{
                            $cipdUpdate['integral_b_del'] = Db::raw('integral_b_del+'.$row[9]);
                        }
                        M('com_integral_prize_buckle')->where('id', $cipb_Id)->update($cipdUpdate);
                    }

                    //积分奖扣事件关联表
                    $c_i_p_b_r_e = [
                        'com_id' => $this->com_id,
                        'prize_buckle_id' => $cipb_Id,
                        'event_id' => $eventId,
                        'create_time' => $time
                    ];
                    $c_i_p_b_r_e_ID = M('com_integral_prize_buckle_relation_event')->insertGetId($c_i_p_b_r_e);

                    //积分奖扣事件参与人关联表
                    $c_i_e_r_u = [
                        'com_id' => $this->com_id,
                        'event_id' => $c_i_p_b_r_e_ID,
                        'user_id' => $userInfo['id'],
                        'integral_b' => $row[9],
                        'integral_a' => $row[8]
                    ];
                    M('com_integral_event_relation_users')->insert($c_i_e_r_u);

                    //添加用户积分
                    if (!empty($row[8])) {
                        $usersUpdate['integral_a'] = Db::raw('integral_a+' . $row[8]);
                    }
                    if (!empty($row[9])) {
                        $usersUpdate['integral_b'] = Db::raw('integral_b+' . $row[9]);
                    }
                    M('users')->where('id', $userInfo['id'])->update($usersUpdate);

                    //添加用户积分记录
                    $c_i_l = [
                        'com_id' => $this->com_id,
                        'user_id' => $userInfo['id'],
                        'type' => 0,
                        'integral_b' => $row[9],
                        'integral_a' => $row[8],
                        'create_time' => $time,
                        'year' => date('Y', $time),
                        'month' => date('m', $time),
                        'year_month' => strtotime(date('Y-m', $time)),
                        'event_id' => $c_i_p_b_r_e_ID,
                    ];
                    M('com_integral_log')->insert($c_i_l);

                    DB::commit();
                    return $this->ajaxReturn(0, '导入成功');
                } catch (\Exception $e) {
                    DB::rollback();
                    return $this->ajaxReturn(1, $e->getMessage());
                }
            }

            //达标固定系统事件
            $this->sys_fixed_thing($cipb_Info, $num);
        }
    }

    private function sys_fixed_thing($info, $num)
    {
        //记录人达到条件奖分
        $sys = M('com_config')->where('com_id',$this->com_id)->field('record_people_num,record_event_num')->find();
        if(!empty($sys['record_people_num'])){//主题人次
            //人次数
            $people_count = count($num);
            $record_people_num = unserialize($sys['record_people_num']);
            //冒泡排序
            $prize_integral = 0;
            $record_people_num = \app\common\lib\SendIntegralNew::data_sort($record_people_num,'people_sum');
            foreach ($record_people_num as $key=>$value){
                if($people_count >=$value['people_sum']){
                    $prize_integral = $value['prize_integral'];
                    break;
                }
            }
            if($prize_integral >0){
                $info['user_id'] = $this->user_id;
                \app\common\lib\SendIntegralNew::add_record_integral($info,$prize_integral,0,23,true);
            }
        }
        if(!empty($sys['record_event_num'])){//主题事件
            //事件数
            $record_event_num = unserialize($sys['record_event_num']);
            //冒泡排序
            $event_prize_integral = 0;
            $record_event_num = \app\common\lib\SendIntegralNew::data_sort($record_event_num,'event_prize_integral');
            foreach ($record_event_num as $key=>$value){
                if($num >=$value['prize_sum']){
                    $event_prize_integral = $value['event_prize_integral'];
                    break;
                }
            }
            if($event_prize_integral >0){
                $info['user_id'] = $this->user_id;
                \app\common\lib\SendIntegralNew::add_record_integral($info,$event_prize_integral,0,24,true);
            }
        }
    }

    //事件视图，导入 author:zj
    public function review_import_csv()
    {
        if ($_FILES['file']['error'] != 0) {
            return $this->ajaxReturn(1, "文件上传有误");
        }
        //文件是否大于2M
        if ($_FILES['file']['size'] > 102400*2) {
            return $this->ajaxReturn(1, "文件最大2M");
        }
        $r = action('file/controller_up');
        if ($r['err'] != 0) {
            return $this->ajaxReturn(1, "文件保存出错");
        }
        $path = ROOT_PATH . 'public' . $r['data']['saveName'];
        $type = 'Xls';//设置为Excel5代表支持2003或以下版本，Excel2007代表2007版
        $xlsReader = IOFactory::createReader($type);
        $xlsReader->setReadDataOnly(true);
        $Sheets = $xlsReader->load($path);
        $dataArray = $Sheets->getSheet(0)->toArray();

        unset($dataArray[0]);       //删除第一行，从第二行开始读取

        unlink($path);
        ldump($dataArray);
        $data = [];
        foreach ($dataArray as $k => $v) {

        }
    }
    //奖票转换
    public function ticket_change(){
        $ids = I('ids/a',[]);
        if(empty($ids)){
            return return_json(1,'id不能为空');
        }
        //查询奖票状态
        $where = [];
        $where['id'] = ['in',$ids];
        $list = M('com_integral_event_relation_users')->where($where)->field('id,is_lottery_ticket')->select();
        DB::startTrans();
        try{
            foreach ($list as $key=>$value){
                if($value['is_lottery_ticket'] == 0){
                    $data = [
                        'is_lottery_ticket'=>1
                    ];
                }else{
                    $data = [
                        'is_lottery_ticket'=>0
                    ];
                }
                M('com_integral_event_relation_users')->where('id',$value['id'])->update($data);
            }
            DB::commit();
            return return_json(0);
        }catch (\Exception $e){
            DB::rollback();
            return return_json(1,$e->getMessage());
        }
    }
    //积分奖扣作废
    public function integral_cancellation(){
        $ids = I('ids/a',[]);
        if(empty($ids)){
            return return_json(1,'id不能为空');
        }
        //查询状态
        $where = [];
        $where_1 = [];
        $where['id'] = ['in',$ids];
        $where_1['id'] = ['in',$ids];
        $where_1['is_cancellation'] = 1;
        $status = M('com_integral_event_relation_users')->where($where_1)->find();
        if(!empty($status)){
            return return_json(1,'请选择正确状态');
        }
        $list = M('com_integral_event_relation_users')->where($where)->field('id,user_id,is_cancellation,integral_a,integral_b')->select();
        DB::startTrans();
        $time = time();
        try{
            foreach ($list as $key=>$value){
                //更新用户积分状态
                $data = [
                    'is_cancellation'=>1
                ];
                M('com_integral_event_relation_users')->where('id',$value['id'])->update($data);
                //修改用户积分
                if($value['integral_b']>0){
                    $data_users = [
                        'integral_a'=>Db::raw('integral_a-'.$value['integral_a']),
                        'integral_b'=>Db::raw('integral_b-'.$value['integral_b']),
                    ];
                }else{
                    $data_users = [
                        'integral_a'=>Db::raw('integral_a+'.abs($value['integral_a'])),
                        'integral_b'=>Db::raw('integral_b+'.abs($value['integral_b'])),
                    ];
                }

//                var_dump($data_users);die;
                M('users')->where('id',$value['user_id'])->update($data_users);
                //添加积分记录
                $c_i_l = [
                    'com_id'=>$this->com_id,
                    'user_id'=>$value['user_id'],
                    'type' => 4,
                    'integral_b'=> -$value['integral_a'],
                    'integral_a'=> -$value['integral_b'],
                    'create_time'=>$time,
                    'year'=>date('Y',$time),
                    'month'=>date('m',$time),
                    'year_month'=>strtotime(date('Y-m',$time)),
                    'leave_type'=>1,
                ];
                M('com_integral_log')->insert($c_i_l);
            }
            DB::commit();
            return return_json(0);
        }catch (\Exception $e){
            DB::rollback();
            return return_json(1,$e->getMessage());
        }
    }
    //积分奖扣恢复
    public function integral_recover(){
        $ids = I('ids/a',[]);
        if(empty($ids)){
            return return_json(1,'id不能为空');
        }
        //查询状态
        $where = [];
        $where_1 = [];
        $where['id'] = ['in',$ids];
        $where_1['id'] = ['in',$ids];
        $where_1['is_cancellation'] = 0;
        $status = M('com_integral_event_relation_users')->where($where_1)->find();
        if(!empty($status)){
            return return_json(1,'请选择正确状态');
        }
        $list = M('com_integral_event_relation_users')->where($where)->field('id,user_id,is_cancellation,integral_a,integral_b')->select();
        DB::startTrans();
        $time = time();
        try{
            foreach ($list as $key=>$value){
                //更新用户积分状态
                $data = [
                    'is_cancellation'=>0
                ];
                M('com_integral_event_relation_users')->where('id',$value['id'])->update($data);
                //修改用户积分
                if($value['integral_b']>0){
                    $data_users = [
                        'integral_a'=>Db::raw('integral_a+'.$value['integral_a']),
                        'integral_b'=>Db::raw('integral_b+'.$value['integral_b']),
                    ];
                }else{
                    $data_users = [
                        'integral_a'=>Db::raw('integral_a-'.abs($value['integral_a'])),
                        'integral_b'=>Db::raw('integral_b-'.abs($value['integral_b'])),
                    ];
                }
                M('users')->where('id',$value['user_id'])->update($data_users);
                //添加积分记录
                $c_i_l = [
                    'com_id'=>$this->com_id,
                    'user_id'=>$value['user_id'],
                    'type' => 4,
                    'integral_b'=> $value['integral_a'],
                    'integral_a'=> $value['integral_b'],
                    'create_time'=>$time,
                    'year'=>date('Y',$time),
                    'month'=>date('m',$time),
                    'year_month'=>strtotime(date('Y-m',$time)),
                    'leave_type'=>2,
                ];
                M('com_integral_log')->insert($c_i_l);
            }
            DB::commit();
            return return_json(0);
        }catch (\Exception $e){
            DB::rollback();
            return return_json(1,$e->getMessage());
        }
    }

}